package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.service.ContentService;
import com.zr.service.impl.ContentServiceImpl;

import net.sf.json.JSONObject;

public class SelectContentAction extends HttpServlet{
	
	ContentService cs = new ContentServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf8");
		req.setCharacterEncoding("utf8");
		String title = req.getParameter("title");
		String center = req.getParameter("center");
		String startDate = req.getParameter("startDate");
		String endDate = req.getParameter("endDate");
		String state = req.getParameter("state");
		if("".equals(title))
		{
			title+="%";
		}
		if("".equals(center))
		{
			center+="%";
		}
		if("".equals(startDate))
		{
			startDate+="0000-00-00";
		}
		if("".equals(endDate))
		{
			endDate+="9999-12-31";
		}
		if("".equals(state))
		{
			state+="%";
		}
		int page = 0;
		int pageSize = 0;
		if(req.getParameter("page")==null||req.getParameter("rows")==null){
			page = 1;
			pageSize = 15;
		}else{
			page = Integer.parseInt(req.getParameter("page"));
			pageSize = Integer.parseInt(req.getParameter("rows"));
		}
		//title知识标题，center知识内容，startDate发布日期,endDate发布日期，state状态
		JSONObject json = cs.getContentBySearch(title, center, startDate, endDate, state,page,pageSize);
		PrintWriter pw = resp.getWriter();
		JSONObject j = new JSONObject();
		int total = Integer.parseInt(json.get("total").toString());
		if(total>0){
			j.put("searchMsg", "有");
			req.getSession().setAttribute("searchList", json);
		}else{
			j.put("searchMsg", "无");
		}
		pw.write(j.toString());
	}
}
